
list_schedules = { }

for mes = 1, 4, 1 do
	for dia = 1, 28, 1 do
		local ItemID = "lunoschedule:schedule_"..mes.."_"..dia
		list_schedules[#list_schedules + 1] = ItemID

		local groupsID = {snappy=2,choppy=2,oddly_breakable_by_hand=2, not_in_creative_inventory=1}
		if mes == 1 and dia == 1 then 
			groupsID = {snappy=2,choppy=2,oddly_breakable_by_hand=2}
		end

		local tileID = "tex_schedule_fundo.png^[combine:200x200"
		if mes==1 then
			tileID = tileID .. ":0,0=tex_schedule_primavera.png"
		elseif mes==2 then
			tileID = tileID .. ":0,0=tex_schedule_verao.png"
		elseif mes==3 then
			tileID = tileID .. ":0,0=tex_schedule_outono.png"
		elseif mes==4 then
			tileID = tileID .. ":0,0=tex_schedule_inverno.png"
		else
			print("[ERRO CRITICO] Nao foi possivel imprimir o nome do mes no calendario!")
		end
		--math.ceil(arredonda para cima de 0.5), math.floor(arredonda para menor de 0.5)
		local row = math.ceil(dia / 7)
		local col = dia-((row-1)*7)
		tileID = tileID .. ":".. math.floor(((col-1)*19.7)+0)..",".. math.floor(((row-1)*16)+0).."=tex_schedule_select.png"
		
		--local groupsID = (mes==1 and dia==1)?{snappy=2,choppy=2,oddly_breakable_by_hand=2}:{snappy=2,choppy=2,oddly_breakable_by_hand=2,not_in_creative_inventory=1}
		
		minetest.register_node(ItemID, {
			description = "Calendario",
			inventory_image = "obj_schedule.png",
			drop = 'lunoschedule:schedule_1_1',
			drawtype = "signlike",
			paramtype = 'light',
			paramtype2 = "wallmounted",
			sunlight_propagates = true,
			walkable = false,
			--tiles = {"tex_schedule_fundo.png^[combine:200x200:0,0=tex_schedule_inverno.png:0,0=tex_schedule_select.png"	},
			tiles = {tileID},
			selection_box = {
				type = "wallmounted",
			},
			--tiles = schedule_tiles,
			--sunlight_propagates = true,
			--light_source = LIGHT_MAX,
			is_ground_content = true, --Nao tenho certeza: Se prega no chao?
			--groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2}, 
			groups = groupsID,
			--sounds = default.node_sound_wood_defaults(), --default.node_sound_glass_defaults(),
			
			on_punch = function(pos, node, puncher, pointed_thing)
				lunoschedule.doUpdateBlockSchedule(pos)
			end,
			after_place_node = function(pos, placer, itemstack)
				lunoschedule.doUpdateBlockSchedule(pos)
			end,
			--[[
			can_dig = function(pos, player)
				local meta = minetest.env:get_meta(pos)
				if meta~=nil then
					local ownername = meta:get_string("owner")
					local playername = player:get_player_name()
					if ownername~=nil and ownername~="" and ownername~=playername then
						minetest.chat_send_player(playername, "Voce nao pode roubar o calendario de "..ownername..".")
						return false
					else
						return true
					end
				else
					return true
				end
			end,
			on_rightclick = function(pos, node, clicker, itemstack)
				local meta = minetest.env:get_meta(pos)
				if meta~=nil then
					local ownername = meta:get_string("owner")
					if ownername~=nil and ownername~="" then
						local ownerplayer = minetest.env:get_player_by_name(ownername)
						local clickername = clicker:get_player_name()
				
						if ownername==clickername then --Verificar se o don oesta online
							lunocartas.openinbox(ownerplayer, meta)
						else
							minetest.chat_send_player(clickername, "Esta caixa de correio nao lhe pertece!")
						end
					end
				end
			end,
			]]--

		})
		minetest.register_alias("lunoschedule:schedule_1_1", ItemID)
	end
end



minetest.register_craft({
	output = 'lunoschedule:schedule_1_1',
	recipe = {
		{"default:paper"	,"default:paper"			,"default:paper"},
		{"default:paper"	,"default:mese_crystal"	,"default:paper"},
		{"default:paper"	,"default:paper"			,"default:paper"},
	}
})

minetest.register_alias("schedule"		,"lunoschedule:schedule_1_1")
minetest.register_alias("calendario"	,"lunoschedule:schedule_1_1")

minetest.register_abm({
	--nodenames = {"calendario"},
	nodenames = list_schedules,
	interval = 15.0,
	chance = 1,
	--action = function(pos)
	action = function (pos, node, active_object_count, active_object_count_wider)
		lunoschedule.doUpdateBlockSchedule(pos)
	end,
})
